Passed
Pull Request — master (#188)
by Mathieu
02:03
created

CreateTaskAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 23
rs 10
c 0
b 0
f 0
wmc 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A index 0 13 2
1
import {
2
  Body,
3
  Post,
4
  Controller,
5
  Inject,
6
  BadRequestException,
7
  UseGuards
8
} from '@nestjs/common';
9
import {AuthGuard} from '@nestjs/passport';
10
import {ApiTags, ApiBearerAuth, ApiOperation} from '@nestjs/swagger';
11
import {CreateTaskCommand} from 'src/Application/Task/Command/CreateTaskCommand';
12
import {ICommandBus} from 'src/Application/ICommandBus';
13
import {TaskDTO} from '../DTO/TaskDTO';
14
import {Roles} from 'src/Infrastructure/HumanResource/User/Decorator/Roles';
15
import {UserRole} from 'src/Domain/HumanResource/User/User.entity';
16
import {RolesGuard} from 'src/Infrastructure/HumanResource/User/Security/RolesGuard';
17
18
@Controller('tasks')
19
@ApiTags('Task')
20
@ApiBearerAuth()
21
@UseGuards(AuthGuard('bearer'), RolesGuard)
22
export class CreateTaskAction {
23
  constructor(
24
    @Inject('ICommandBus')
25
    private readonly commandBus: ICommandBus
26
  ) {}
27
28
  @Post()
29
  @Roles(UserRole.COOPERATOR, UserRole.EMPLOYEE)
30
  @ApiOperation({summary: 'Create new task'})
31
  public async index(@Body() taskDto: TaskDTO) {
32
    try {
33
      const id = await this.commandBus.execute(
34
        new CreateTaskCommand(taskDto.name)
35
      );
36
37
      return {id};
38
    } catch (e) {
39
      throw new BadRequestException(e.message);
40
    }
41
  }
42
}
43